﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using MDL;

namespace DAL
{
    public class DAL_Admins
    {
        /// <summary>
        /// 查询登录是否成功
        /// </summary>
        /// <param name="s"></param>
        /// <returns>登录人信息</returns>
        public static Admins LoginSelect(Admins s)
        {
            string sql = "select * from admins where UserTypeID =@UserTypeID and UserName =@UserName and UserPWD =@UserPWD";
            SqlParameter[] sprs = { 
                                  new SqlParameter("@UserTypeID",s.UserTypeID),
                                  new SqlParameter("@UserName",s.UserName),
                                  new SqlParameter("@UserPWD",s.UserPwd)
                                  };
            using (SqlDataReader dr = DBHelper.白道(sql, sprs, CommandType.Text))
                if (dr.Read())
                {
                    Admins a = new Admins();
                    a.UserID = Convert.ToInt32(dr[0]);
                    a.UserName = dr[1].ToString();
                    return a;
                }
                else
                {
                    return null;
                }
        }
        /// <summary>
        /// 解锁查询登录信息
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns>是否有</returns>
        public static Admins JiSuoSelect(string name, string pwd)
        {
            string sql = "select * from admins where UserName =@UserName and UserPWD =@UserPWD";
            SqlParameter[] sprs = { 
                                  new SqlParameter("@UserName",name),
                                  new SqlParameter("@UserPWD",pwd)
                                  };
            using (SqlDataReader dr = DBHelper.白道(sql, sprs, CommandType.Text))
                if (dr.Read())
                {
                    Admins a = new Admins();
                    a.UserID = Convert.ToInt32(dr[0]);
                    a.UserName = dr[1].ToString();
                    return a;
                }
                else
                {
                    return null;
                }
        }
        /// <summary>
        /// 查询管理信息
        /// </summary>
        /// <returns>管理信息</returns>
        public static List<Admins> SelectAdmin()
        {
            string sql = "select * from admins";
            List<Admins> list = new List<Admins>();
            using (SqlDataReader dr = DBHelper.白道(sql, null, CommandType.Text))
            {
                while (dr.Read())
                {
                    Admins a = new Admins();
                    a.UserID = Convert.ToInt32(dr[0]);
                    a.UserName = dr[1].ToString();
                    a.UserCompellation = dr[3].ToString();
                    list.Add(a);
                }
            }
            return list;
        }
        /// <summary>
        /// 增加用户名
        /// </summary>
        /// <param name="yname">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="zname">真实姓名</param>
        /// <returns>受影响的行数</returns>
        public static int AddAdmin(string yname, string pwd, string zname)
        {
            string sql = "insert into admins values(@yname,@pwd,@zname,1)";
            SqlParameter[] sprs = { 
                                  new SqlParameter("@yname",yname),
                                  new SqlParameter("@pwd",pwd),
                                  new SqlParameter("@zname",zname)
                                  };
            return DBHelper.增删改(sql, sprs, CommandType.Text);
        }
        /// <summary>
        /// 修改管理员信息
        /// </summary>
        /// <param name="yname">用户名</param>
        /// <param name="zname">真实姓名</param>
        /// <param name="id">根据用户ID</param>
        /// <returns>受影响的行数</returns>
        public static int UpdateAdmin(string yname, string zname, int id)
        {
            string sql = "update admins set UserName = @yname,UserCompellation = @zname where UserID = @id";
            SqlParameter[] sprs = { 
                                  new SqlParameter("@yname",yname),
                                  new SqlParameter("@zname",zname),
                                  new SqlParameter("@id",id)
                                  };
            return DBHelper.增删改(sql, sprs, CommandType.Text);
        }
        /// <summary>
        /// 查询是否已存在次用户名
        /// </summary>
        /// <param name="name">根据用户名</param>
        /// <returns>是否有</returns>
        public static List<Admins> SelectAdmins(string name)
        {
            string sql = "select * from admins where UserName = @name";
            SqlParameter[] sprs = { new SqlParameter("@name", name) };
            List<Admins> list = new List<Admins>();
            using (SqlDataReader dr = DBHelper.白道(sql, sprs, CommandType.Text))
            {
                while (dr.Read())
                {
                    Admins a = new Admins();
                    a.UserID = Convert.ToInt32(dr[0]);
                    list.Add(a);
                }
            }
            return list;
        }
        /// <summary>
        /// 删除管理信息
        /// </summary>
        /// <param name="id">根据用户ID</param>
        /// <returns>受影响的行数</returns>
        public static int DeleteAdmin(int id)
        {
            string sql = "delete from admins where UserID = @id";
            SqlParameter[] sprs = { new SqlParameter("@id", id) };
            return DBHelper.增删改(sql, sprs, CommandType.Text);
        }
        /// <summary>
        /// 下拉绑定操作人信息
        /// </summary>
        /// <returns></returns>
        public static List<Admins> SelectXl()
        {
            string sql = "select UserID,UserName from admins";
            List<Admins> list = new List<Admins>();
            using (SqlDataReader dr = DBHelper.白道(sql, null, CommandType.Text))
            {
                while (dr.Read())
                {
                    Admins a = new Admins();
                    a.UserID = Convert.ToInt32(dr[0]);
                    a.UserName = dr[1].ToString();
                    list.Add(a);
                }
            }
            return list;
        }
        /// <summary>
        /// 查询等级人类型ID
        /// </summary>
        /// <param name="id">类型ID</param>
        /// <returns></returns>
        public static List<Admins> SelectQuanXie(int id)
        {
            string sql = "select * from admins where UserTypeID != @id";
            SqlParameter[] sprs = { new SqlParameter("@id", id) };
            List<Admins> list = new List<Admins>();
            using (SqlDataReader dr = DBHelper.白道(sql, sprs, CommandType.Text))
            {
                while (dr.Read())
                {
                    Admins a = new Admins();
                    a.UserID = Convert.ToInt32(dr[0]);
                    list.Add(a);
                }
            }
            return list;
        }
        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="XPwd">新密码</param>
        /// <param name="Name">根据用户名</param>
        /// <param name="JPwd">根据原密码</param>
        /// <returns>受影响的行数</returns>
        public static int UpdatePwd(string XPwd, string Name, string JPwd)
        {
            string sql = "update admins set UserPWD = @XPwd where UserName =@Name and UserPWD =@JPwd";
            SqlParameter[] sprs = { 
                                      new SqlParameter("@XPwd", XPwd),
                                      new SqlParameter("@Name", Name),
                                      new SqlParameter("@JPwd", JPwd)
                                  };
            return DBHelper.增删改(sql,sprs,CommandType.Text);
        }
    }
}
